﻿using System;
using System.Web;
using System.Security.Principal;
using System.Data;
using Portal.User.Security;
namespace Portal 
{
	/// <summary>
	/// Summary description for Global.
	/// </summary>
	public class Global : System.Web.HttpApplication
	{
		public Global()
		{
			InitializeComponent();
		}	
		/// <summary>
		/// Thuc hien Install mot so file cau hinh neu he thong khong co.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void Application_Start(Object sender, EventArgs e)
		{
			//Portal.API.Helper.InstallFile("Portal.config","settings/","Installs/");
			//Portal.API.Helper.InstallFile("Portal.css");
			//Portal.API.Helper.InstallFile("PortalFooter.ascx");
			//Portal.API.Helper.InstallFile("PortalHeader.ascx");
			//Portal.API.Helper.InstallFile("Users.config");
            //if(Portal.API.Helper.InstallFile("web.config"))
            //{
            //    throw new Exception("web.config file installed, reload the application");
            //}
		}
		protected void Session_Start(Object sender, EventArgs e)
		{
		}

		protected void Application_BeginRequest(Object sender, EventArgs e)
		{

		}

		protected void Application_EndRequest(Object sender, EventArgs e)
		{

		}

		protected void Application_AuthenticateRequest(Object sender, EventArgs e)
		{
            if (Request.IsAuthenticated)
            {
                string[] roles = null;
                MainSecurity objscu = new MainSecurity();
                roles = objscu.GetRoleSymbol(HttpContext.Current.User.Identity.Name, Portal.API.Config.CurrentChannel);
               
                //ChannelUsers objUser = new ChannelUsers();
                //string[] roles = objUser.getRoles(HttpContext.Current.User.Identity.Name);//UserManagement.GetRoles(HttpContext.Current.User.Identity.Name);
                HttpContext.Current.User = new GenericPrincipal(HttpContext.Current.User.Identity, roles);
            }
		}

		protected void Application_Error(Object sender, EventArgs e)
		{

		}

		protected void Session_End(Object sender, EventArgs e)
		{
		}
		protected void Application_End(Object sender, EventArgs e)
		{

		}

		public override string GetVaryByCustomString(HttpContext context, string custom)
		{
			if ( custom.ToLower() =="cachingmultiversion")
			{
				HttpCookie cookie = context.Request.Cookies["CachingMultiVersion"]; 
				if(cookie != null)  return cookie.Value; 
			}
			return base.GetVaryByCustomString (context, custom);
		}
		public static string CachingMultiVersion
		{
			set 
			{
				System.Web.HttpContext.Current.Response.Cookies["CachingMultiVersion"].Value=value;
			}
		}
			
		#region Web Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
		}
		#endregion
	}
}

